import React, { useState, useEffect } from 'react';
import { Goback } from './Goback';
import instance from '../utils/axios';
import tool from '../utils/format'

import styled from 'styled-components'

const Box = styled.div`
.index{
    box-sizing: border-box;
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
}
.head{
    height: 40px;
    width: 100%;
    background-color: #dd3635;
    display: flex;
    box-sizing: border-box;
    padding-left: 10px;
    padding-right: 10px;
    align-items: center;
}
.content{
    flex: 1;
    overflow-y: auto;
    box-sizing: border-box;
    padding-left: 10px;
    padding-right: 10px;
}


.content>ul>li{
    display: flex;
    height: 100px;
    border-bottom: 1px solid #f3f3f3;
    box-sizing: border-box;
    padding-top: 14px;
    padding-bottom: 14px;
}
.content>ul>li:last-child{
    border-bottom: 0;
}

.info{
    flex: 1;
    display: inline-flex;
    flex-direction: column;
    justify-content: space-between;
    margin-right: 10px;
}


.title{
    font-size: 18px;
    color: #222222;
    overflow:hidden;
    display:-webkit-box;
    text-overflow:ellipsis;
    -webkit-line-clamp:2; 
    -webkit-box-orient:vertical; 
    white-space:normal;
}

.something{
    color: #a5a5a5;
    font-size: 12px;
}


.img{
    width: 115px;
    height: 72px;
    overflow: hidden;
}

.img>img{
    width: 100%;
}

.back_btn{
    color: #ffffff;
    font-size: 16px;
}

.back_btn::before{
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    border-left: 3px solid #ffffff;
    border-top: 3px solid #ffffff;
    transform: rotate(-45deg);
}
`


const Collection = (props) => {
    const [list, setList] = useState([]);
    let user = JSON.parse(localStorage.getItem('user'))

    useEffect(() => {
        instance.post('clist', { uid: user._id }).then(data => {
            // console.log(data)
            setList(data)
        })
    }, [])

    return (
        <Box>
            <div className="index">
                <div className="head">
                    <Goback></Goback>
                </div>
                <div className="content">
                    <ul>
                        {
                            list.map((item, index) => {
                                if (item.type === '财经') {
                                    return (
                                        <li key={item._id} onClick={() => {
                                            props.history.push('/newsinfo/' + item.aid._id)
                                        }}>
                                            <div className="info">
                                                <div className="title">{item.aid.title}</div>
                                                <div className="something">
                                                    <span className="m_r10">{item.aid.media}-{item.aid.type}</span>
                                                    <span>{tool.toYMD(item.aid.pubtime)}</span>
                                                </div>
                                            </div>
                                            <div className="img">
                                                {item.aid.img && item.aid.img !== "none" ? <img src={item.aid.img} /> : <div className="noimg">暂无缩略图</div>}
                                            </div>
                                        </li>
                                    )
                                } else {
                                    return (
                                        <li key={item.aid._id} onClick={() => {
                                            props.history.push('/newsinfo/' + item.aid._id)
                                        }}>
                                            <div className="info">
                                                <div className="title">{item.aid.title}</div>
                                                <div className="something">
                                                    <span className="m_r10">{item.aid.source}-{item.aid.type}</span>
                                                    <span>{tool.toYMD(item.aid.time)}</span>
                                                </div>
                                            </div>
                                            <div className="img">
                                                {item.aid.img && item.aid.img !== "none" ? <img src={item.aid.img} /> : <div className="noimg">暂无缩略图</div>}
                                            </div>
                                        </li>
                                    )
                                }
                            })
                        }
                    </ul>
                </div>

            </div>
        </Box>
    )
}
export default Collection